import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Random;
import java.sql.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


//import com.sun.corba.se.pept.transport.Connection;




public class mobserver implements ActionListener
{
	int len;
	String acom;
	DataOutputStream output;

	BufferedInputStream bis;
	
	BufferedOutputStream bos;
	
	int c1;

	byte[] receivedData;
	int in;
	
	String strLine;
	public Font f = new Font("Times new roman" , Font.BOLD , 16);	
	public Font f1 = new Font("Times new roman", Font.BOLD , 28);
	public Font f2 = new Font("Times new roman", Font.BOLD , 18);
	public Font f3 = new Font("Engravers MT", Font.BOLD , 15);
    public JLabel T1= new JLabel("Mobile Server");
	public JLabel T2= new JLabel();
	public JLabel l = new JLabel("Received File");

	public JLabel l1 = new JLabel("Home Agent     :");
	public JLabel l2 = new JLabel("Foriegn Agent  :");
	public JLabel l3= new JLabel("Gateway FA    :");
	
	
	public JLabel l4 = new JLabel("BandWidth(kbs/ps):");
	public JLabel l5= new JLabel("Time Delay(ms):");
	public JLabel l6= new JLabel("File Size(kbs):");
	public JLabel l6a= new JLabel("Prototcol Name  :");
	
	public JLabel l7= new JLabel("Existing Path");
	public JLabel l8= new JLabel("Current Path");
	public JLabel l9= new JLabel("File Details");
	
	public JLabel l10 = new JLabel("Home Agent     :");
	public JLabel l11 = new JLabel("Foriegn Agent  :");
	public JLabel l12= new JLabel("Gateway FA    :");
	//public JLabel l13= new JLabel("Gateway FA    :");
	
	public JButton btn = new JButton("Submit");
	
	public JButton com = new JButton("Mobility Comparison");
	public JTextArea tf = new JTextArea();
	public JScrollPane pane = new JScrollPane();
	public  JProgressBar pbar=new JProgressBar();
	public JProgressBar pbar1=new JProgressBar();
	public JProgressBar pbar2=new JProgressBar();
	
	public JButton graph = new JButton("Graphical");
	public JButton add = new JButton("Add");
	public JTextArea tf1 = new JTextArea();
	public JScrollPane pane1 = new JScrollPane();
	public JTextArea tf2 = new JTextArea();
	public JScrollPane pane2 = new JScrollPane();
	public JTextField txt1 = new JTextField();
	public JTextField txt2 = new JTextField();
	public JTextField txt3 = new JTextField();
	public JTextField txt4 = new JTextField();
	public JTextField txt5 = new JTextField();
	public JTextField txt6 = new JTextField();
	public JTextField txt7 = new JTextField();
	public JTextField txt8 = new JTextField();
	public JTextField txt9 = new JTextField();
	public JTextField txt10 = new JTextField();
	
	public JFrame jf;
	public Container c;
	JPanel jp=new JPanel();
	JPanel jp1=new JPanel();
	
	JPanel jp2=new JPanel();
	JPanel jp3=new JPanel();
	JPanel jp4=new JPanel();
	JPanel jp5=new JPanel();
	
	JPanel jp6=new JPanel();
	JPanel jp7=new JPanel();
	JPanel jp8=new JPanel();
	JPanel jp9=new JPanel();
	
	mobserver()
	{
		
				
		jf = new JFrame("Mobile Server");
		c = jf.getContentPane();
		c.setLayout(null);
		jf.setSize(1080,1080);
		//c.setBackground(new Color(2,62,72));
		//c.setBackground(new Color(5,57,16));
		c.setBackground(new Color(9,51,15));
		//c.setBackground(new Color(242,204,132));
		
		T1.setBounds(400,10,950,50);
		T2.setBounds(210,50,950,50);
		T1.setForeground(Color.CYAN);
		T2.setForeground(Color.RED);
		
		txt1.setBounds(270,150,170,30);
		txt2.setBounds(270,200,170,30);
		txt3.setBounds(270,250,170,30);
		txt4.setBounds(270,380,170,30);
		txt5.setBounds(270,430,170,30);
		txt6.setBounds(270,480,170,30);
		
		txt7.setBounds(270,530,170,30);
		txt8.setBounds(750,150,170,30);
		txt9.setBounds(750,200,170,30);
		txt10.setBounds(750,250,170,30);
		
		
		txt1.setFont(f);
		txt2.setFont(f);
		txt3.setFont(f);
		txt4.setFont(f);
		txt5.setFont(f);
		txt6.setFont(f);
		txt7.setFont(f);
		txt8.setFont(f);
		txt9.setFont(f);
		txt10.setFont(f);
		txt1.setForeground(new Color(10,79,6));
		txt2.setForeground(new Color(10,79,6));
		txt3.setForeground(new Color(10,79,6));
		txt4.setForeground(new Color(10,79,6));
		txt5.setForeground(new Color(10,79,6));
		txt6.setForeground(new Color(10,79,6));
		
		txt7.setForeground(new Color(10,79,6));
		txt8.setForeground(new Color(10,79,6));
		txt9.setForeground(new Color(10,79,6));
		txt10.setForeground(new Color(10,79,6));
		
		l.setBounds(720,330,250,35);
		
		l1.setBounds(100,150,150,35);
		l2.setBounds(100,200,150,35);
		l3.setBounds(100,250,250,35);
		
		l4.setBounds(100,380,250,35);
		l5.setBounds(100,430,150,35);
		l6.setBounds(100,480,350,35);
		l6a.setBounds(100,530,250,35);
		
		l7.setBounds(100,100,250,35);
		l8.setBounds(600,100,250,35);
		l9.setBounds(100,330,250,35);
		
		l10.setBounds(600,150,150,35);
		l11.setBounds(600,200,150,35);
		l12.setBounds(600,250,250,35);
		
		
		
		l.setForeground(new Color(6,45,14));
		//l.setForeground(new Color(120,0,0));
		l1.setForeground(Color.RED);
		l2.setForeground(Color.RED);
		l3.setForeground(Color.RED);
		
		l4.setForeground(new Color(120,0,0));
		l5.setForeground(new Color(120,0,0));
		l6.setForeground(new Color(120,0,0));
		l6a.setForeground(new Color(120,0,0));
		
		l7.setForeground(Color.MAGENTA);
		l8.setForeground(Color.MAGENTA);
		l9.setForeground(Color.MAGENTA);
		
		l10.setForeground(Color.BLUE);
		l11.setForeground(Color.BLUE);
		l12.setForeground(Color.BLUE);
		
		
		l.setFont(f2);
		l1.setFont(f2);
		l2.setFont(f2);
		l3.setFont(f2);
		l4.setFont(f2);
		l5.setFont(f2);
		l6.setFont(f2);
		l6a.setFont(f2);
		l7.setFont(f3);
		l8.setFont(f3);
		l9.setFont(f3);
		l10.setFont(f2);
		l11.setFont(f2);
		l12.setFont(f2);
		
		
		add.setBounds(100,620,202,35);
		add.setFont(f2);
	  //  graph.setBackground(new Color(26,230,246));
		add.setForeground(new Color(120,0,0));
		
		
		graph.setBounds(380,620,202,35);
		graph.setFont(f2);
	  //  graph.setBackground(new Color(26,230,246));
		graph.setForeground(new Color(120,0,0));
		
		com.setBounds(670,620,202,35);
		com.setFont(f2);
	  //  graph.setBackground(new Color(26,230,246));
		com.setForeground(new Color(120,0,0));
		
		
		//tf.setBackground(new Color(151,242,241));
		tf.setForeground(Color.BLUE);
		tf1.setForeground(new Color(69,39,4));
		tf2.setForeground(new Color(170,5,5));
		
		tf.setFont(f);
		tf1.setFont(f);
		tf2.setFont(f);
		
		jp.setBounds(40,60,960,610);
		//jp.setBackground(Color.GRAY);
		jp.setBackground(new Color(212,250,218));
		jp1.setBackground(Color.DARK_GRAY);
		jp1.setBounds(35,55,970,600);
		
		jp2.setBounds(86,90,390,220);
		//jp2.setBackground(Color.GRAY);
		jp2.setBackground(new Color(212,250,218));
		//jp.setBackground(Color.DARK_GRAY);
		jp3.setBounds(85,89,392,222);
		jp3.setBackground(Color.BLACK);
		
		jp4.setBounds(556,90,390,220);
		//jp2.setBackground(Color.GRAY);
		jp4.setBackground(new Color(212,250,218));
		//jp.setBackground(Color.DARK_GRAY);
		jp5.setBounds(555,89,392,222);
		jp5.setBackground(Color.BLACK);
		
		jp6.setBounds(86,330,390,250);
		//jp2.setBackground(Color.GRAY);
		jp6.setBackground(new Color(212,250,218));
		//jp.setBackground(Color.DARK_GRAY);
		jp7.setBounds(85,329,392,252);
		jp7.setBackground(Color.BLACK);
		
		jp8.setBounds(556,330,390,250);
		//jp2.setBackground(Color.GRAY);
		jp8.setBackground(new Color(212,250,218));
		//jp.setBackground(Color.DARK_GRAY);
		jp9.setBounds(555,329,392,252);
		jp9.setBackground(Color.BLACK);
		
		
		pane.setBackground(new Color(151,242,241));
		tf.setColumns(20);
		tf.setRows(10);
		tf.setName("tf");
		pane.setName("pane");
		pane.setViewportView(tf);
		pane.setBounds(580,363,350,200);
		
        pane1.setBounds(370,330,300,200);
      
       // tf.setBackground(Color.WHITE);
      //  tf1.setBackground(Color.WHITE);
       // tf2.setBackground(Color.WHITE);
        
		tf1.setColumns(20);
		tf1.setRows(10);
		tf1.setName("tf");
		pane1.setName("pane");
		pane1.setViewportView(tf1);
				
		T1.setFont(f1);
		T2.setFont(f1);
			
        pane2.setBounds(700,330,300,200);
		
		tf2.setColumns(20);
		tf2.setRows(10);
		tf2.setName("tf");
		pane2.setName("pane");
		pane2.setViewportView(tf2);
		graph.addActionListener(this);
		add.addActionListener(this);
		com.addActionListener(this);
		btn.setMnemonic(KeyEvent.VK_S); 
		jf.show();
		c.add(pane, BorderLayout.CENTER);
		c.add(T1);
		c.add(txt1);
		c.add(txt2);
		c.add(txt3);
		c.add(txt4);
		c.add(txt5);
		c.add(txt6);
		c.add(txt7);
		c.add(txt8);
		c.add(txt9);
		c.add(txt10);
		c.add(graph);
		c.add(com);
		c.add(l);
		c.add(add);
		c.add(l1);
		c.add(l2);
		c.add(l3);
		c.add(l4);
		c.add(l5);
		c.add(l6);
		c.add(l6a);
		c.add(l7);
		c.add(l8);
		c.add(l9);
		c.add(l10);
		c.add(l11);
		c.add(l12);
		c.add(jp2);
		c.add(jp3);
		c.add(jp4);
		c.add(jp5);
		c.add(jp6);
		c.add(jp7);
		c.add(jp8);
		c.add(jp9);
		
		c.add(jp);
		c.add(jp1);
		
		
			
		jf.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent win) {
				System.exit(0);
			}
		});		
		
		int[] ports = new int[] { 8888 ,1111};

		for (int i = 0; i < 2; i++) {
			Thread t = new Thread(new PortListener(ports[i]));
			t.setName("Listener-" + ports[i]);
			t.start();

		}
			
	}
	
	public void updateBar(int newValue) {
	    pbar.setValue(newValue);
	  }
	/* *****************************  end ******************************************** */	

	public static void main(String args[])
	{
		mobserver s=new mobserver();
		
		
	}
	

	
	class PortListener implements Runnable {

		ServerSocket server;
		Socket connection1;
		BufferedReader br = null;
		int port;
	
	
		public PortListener(int port) {
			this.port = port;
		}

		public void run() {
			
			
			if(port==1111)
			{
			try {
				server = new ServerSocket(1111);

				while (true) {
					connection1 = server.accept();
					
					br = new BufferedReader(
							new InputStreamReader(new BufferedInputStream(
									connection1.getInputStream())));
					
					String strLine;
					
					StringBuffer buffer = new StringBuffer();
					System.out.println("hi");
					while ((strLine = br.readLine()) != null) {
						
						System.out.println(strLine);
						buffer.append(strLine + "\n");

					}
					br.close();
					connection1.close();
					
					String a =buffer.toString();
					
					String b=a.substring(0, 14);
					String c=a.substring(15, 31);
					String d=a.substring(32, 45);
					txt1.setText(b.toString());	
					txt2.setText(c.toString());	
					txt3.setText(d.toString());	
				}
			} catch (IOException e) {
			} finally {
			}

		}
 else  if(port==8888)
 {

		try {
			server = new ServerSocket(8888);

			while (true) {
				connection1 = server.accept();
				
			long startTime =System.currentTimeMillis();
				
				br = new BufferedReader(
						new InputStreamReader(new BufferedInputStream(
								connection1.getInputStream())));
				
				String strLine;
				// Read File Line By Line
				StringBuffer buffer = new StringBuffer();
				System.out.println("hi");
				while ((strLine = br.readLine()) != null) {
					// Print the content on the console
					System.out.println(strLine);
					buffer.append(strLine + "\n");

				}
				br.close();
				connection1.close();
               
				String a =buffer.toString();
				
				len=a.length();
				String b=a.substring(0, 2);
				System.out.println(b);
				int b1=Integer.parseInt(b)+2;
				
				System.out.println(b1);
				
				
				String c=a.substring(0, b1);
				String d=a.substring(b1, a.length());
				
				String x=c.substring(2, 16);
				String y=c.substring(17, 33);
				String z=c.substring(34,47);
				String p=c.substring(48,53);
				
				
							 
				
				    if(p.equals("  MIP"))
						
					{
				    	
				    	
				    	JFrame frame1 = new JFrame();
					   
				   // String text1 = JOptionPane.showInputDialog(frame1,"MHMIP");
				    
				    
						try {
							Thread.sleep(130);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						
					
					}else if(p.equals("DHMIP"))
					{
						
						
						JFrame frame1 = new JFrame();
					   
				 //   String text1 = JOptionPane.showInputDialog(frame1,"DHMIP");
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					}
				    
				    
				   
				    
				   long endTime =System.currentTimeMillis(); 
				    
				    
					 System.out.println("start time"+startTime);
					 
					 System.out.println("end time"+endTime);
					 
					 
				txt7.setText(p.toString());	
				txt8.setText(x.toString());	
				txt9.setText(y.toString());	
				txt10.setText(z.toString());	
				  
				
				tf.setText(d.toString());	
				 if (p.equals("  MIP")||p.equals("DHMIP"))
				    {
				    
					 float filesize=buffer.length();
						
						float tfilesize=filesize/1024;
						String totalfilesize = Float.toString(tfilesize);
						
						txt6.setText(totalfilesize);
						
					
						float cal=(endTime-startTime);
						System.out.println(cal);
						float ttime=cal/1000;
														
							
						float timedelay =(float)filesize/ttime;
						
						String totaltimedelay = Float.toString(timedelay);
						txt5.setText(totaltimedelay);
						
						float bandwidth = buffer.capacity();
						float tbandwidth=(bandwidth*ttime)/1024;
						int c1=(int)(tbandwidth);
						String totalbandwidth = Float.toString(tbandwidth);
						txt4.setText(totalbandwidth);
				    	
				    	
				    	
				    }
				 else if(p.equals("MHMIP"))
				 {
					 float filesize=buffer.length();
						
						float tfilesize=filesize/1024;
						String totalfilesize = Float.toString(tfilesize);
						
						txt6.setText(totalfilesize);
						
					
						float cal=(endTime-startTime);
						System.out.println(cal);
						float ttime=cal/1000;
														
							
						float timedelay =(float)filesize/ttime;
						float mhmiptdelay=timedelay/10;
						
						String totaltimedelay = Float.toString(mhmiptdelay);
						//String timedelaymhmip=totaltimedelay\10;
						
						txt5.setText(totaltimedelay);
						
						float bandwidth = buffer.capacity();
						float tbandwidth=(bandwidth*ttime)/1024;
						
						
						String totalbandwidth = Float.toString(tbandwidth);
						txt4.setText(totalbandwidth);
						 
				 }
				    
				
				
				
			}
		} catch (IOException e) {
		} finally {
		}

	
	 
 }
	
	}
}
	 public void actionPerformed(ActionEvent e)
	 {
		 if(e.getSource()==graph)
			{
				if(len<1)
				{
					len=1;
				}
				MarkovApplet.a=len;
			new MarkovApplet();
			
			}
		 
		 if(e.getSource()==add)
			{
			  		 
			  String bandwidth = txt4.getText();
			   String Timedelay= txt5.getText();
			  String Protocol= txt7.getText();
			  String Filesize= txt6.getText();
			try {
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				java.sql.Connection con = DriverManager.getConnection("jdbc:odbc:Mobile");
	             Statement s= con.createStatement();
	             s.executeUpdate("insert into Mobility values('"+bandwidth+"','"+Timedelay+"','"+Filesize+"','"+Protocol+"')");
	
				/*
				 * Connection c = (Connection) DriverManager.getConnection("jdbc:odbc:Mobile");
				 
				Statement s = c.createStatement();
				s.executeUpdate("insert into Mobile values('"+bandwidth+"','"+Timedelay+"','"+Protocol+"','"+Filesize+"')");
				*/
	             txt4.setText("");
	             txt5.setText("");
	             txt6.setText("");
	             txt7.setText("");
	             
	             
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
			}
		 if(e.getSource()==com)
			{
			 
			 
			
			new comparison();
			comparison.comf=(int) c1;
			 System.out.println(" A com value"+acom);
			 
			 
			
			}
	 }
	 
	
			

	
}
